<?php
class NewCategoriesController extends AppController {

	var $name = 'NewCategories';
	
	

	function index() {
		$this->NewCategory->recursive = 0;
		$this->set('newCategories', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid new category', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('newCategory', $this->NewCategory->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->NewCategory->create();
			if ($this->NewCategory->save($this->data)) {
				$this->Session->setFlash(__('The new category has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The new category could not be saved. Please, try again.', true));
			}
		}
		$this->set('list_cat',$this->_find_list());
		//$parentNewCategories = $this->NewCategory->ParentNewCategory->find('list');
		//$this->set(compact('parentNewCategories'));
	}
	function _find_list() {
		return $this->NewCategory->generatetreelist(null, null, null, '__');
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid new category', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->NewCategory->save($this->data)) {
				$this->Session->setFlash(__('The new category has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The new category could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->NewCategory->read(null, $id);
		}
		$parentNewCategories = $this->NewCategory->ParentNewCategory->find('list');
		$this->set(compact('parentNewCategories'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for new category', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->NewCategory->delete($id)) {
			$this->Session->setFlash(__('New category deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('New category was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>